package com.example.codelabf;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.ExecutionException;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.example.codelabf.dummy.DummyContent;

/**
 * A fragment representing a single Pic detail screen. This fragment is either
 * contained in a {@link PicListActivity} in two-pane mode (on tablets) or a
 * {@link PicDetailActivity} on handsets.
 */
public class PicDetailFragment extends Fragment {
	/**
	 * The fragment argument representing the item ID that this fragment
	 * represents.
	 */
	public static final String ARG_ITEM_ID = "item_id";

	/**
	 * The dummy content this fragment is presenting.
	 */
	private DummyContent.DummyItem mItem;

	/**
	 * Mandatory empty constructor for the fragment manager to instantiate the
	 * fragment (e.g. upon screen orientation changes).
	 */
	public PicDetailFragment() {
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		if (getArguments().containsKey(ARG_ITEM_ID)) {
			// Load the dummy content specified by the fragment
			// arguments. In a real-world scenario, use a Loader
			// to load content from a content provider.
			mItem = DummyContent.ITEM_MAP.get(getArguments().getString(
					ARG_ITEM_ID));
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		final String LOG_TAG = "onCreateView";
		View rootView = inflater.inflate(R.layout.fragment_pic_detail,
				container, false);

		// Show the content in an ImageView.
		if (mItem != null) {
            Bitmap b = null;
            GetImageTask git = new GetImageTask();
            try {
                b = git.execute(mItem.content).get();
                ((ImageView) rootView.findViewById(R.id.pic_detail_pic))
                        .setImageBitmap(b);
            } catch (InterruptedException e) {
	            Log.e(LOG_TAG, "Interrupted Exception", e);
            } catch (ExecutionException e) {
	            Log.e(LOG_TAG, "Execution Exception", e);
            }
		}

		return rootView;
	}

	public final class GetImageTask extends AsyncTask<String, Void, Bitmap> {
		private static final String LOG_TAG = "GetImageTask";
		
	    @Override
	    protected Bitmap doInBackground(String... params) {
	        String picUrlStr = params[0];
	        URL picUrl;
	        InputStream is;
	        Bitmap b = null;
	        
	        if (TextUtils.isEmpty(picUrlStr)) {
	            return null;
	        }
	        
	        try {
	            picUrl = new URL(picUrlStr);
	            is = (InputStream) picUrl.getContent();
	            b = BitmapFactory.decodeStream(is);
	        } catch (MalformedURLException e) {
	            Log.e(LOG_TAG, "Malformed URL", e);
	        } catch (IOException e) {
	            Log.e(LOG_TAG, "IO Exception", e);
	        }
	        return b;
	    }
	} // GetImageTask
}
